package cn.edu.zju.gis.disasterBE.sever.dao;

import cn.edu.zju.gis.disasterBE.sever.entity.User;
import cn.edu.zju.gis.disasterBE.sever.entity.global.MyResponse;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface UserDAO extends BaseMapper<User> {
    @Select("select * from users where uid = #{id}")
    User getByID(Integer id);

    @Select("select * from users where mail_addr = #{mailAddr}")
    User getByMail(String mailAddr);

    @Select("select * from users where nick_name = #{nickName}")
    User getByNickName(String nickName);

    @Update("update users set pwd = #{toUpdateValue} where uid = #{uid}")
    int updatePwdByUID(String toUpdateValue, Integer uid);

    @Update("update users set utype = 1 where uid = #{uid}")
    int updateUtypeByUID(Integer uid);

    default boolean isExistMail(String mailAddr)
    {
        try {
            User user = getByMail(mailAddr);//如果成功找到了，那么不会被捕捉异常，直接返回true
            if(user == null)    return false;
        }catch (NullPointerException e)
        {
            return false;
        }
        return true;
    }

    default boolean isExistNM(String nick_name)
    {
        try {
            User user = getByNickName(nick_name);//如果成功找到了，那么不会被捕捉异常，直接返回true
            if(user == null)    return false;
        }catch (NullPointerException e)
        {
            return false;
        }
        return true;
    }
    default boolean registerUser(User user)
    {
        if(isExistNM(user.getNickName()) && isExistMail(user.getMailAddr()))
            return false;
        user.setUType(0);
        System.out.println(user);
        return this.insert(user)>0;
    }

    default MyResponse<User> updateUser(User user)
    {
        MyResponse<User> myResponse = new MyResponse<>();
        myResponse.setState(false);
        try {
            User tmp = getByMail(user.getMailAddr());
            user.setUid(tmp.getUid());
            if(user.getAddress1() == null)
            {
                user.setAddress1(tmp.getAddress1());
            }
            if(user.getPwd() == null)
            {
                user.setAddress1(tmp.getAddress1());
            }
            if(user.getPwd() == null)
            {
                user.setAddress1(tmp.getAddress1());
            }
            if (user.getFirstName() == null)
            {
                user.setFirstName(tmp.getFirstName());
            }
            if (user.getLastName() == null)
            {
                user.setLastName(tmp.getLastName());
            }
            user.setUType(tmp.getUType());

        }catch (NullPointerException e)
        {
            myResponse.setMsg("The account doesn't exist");
            return myResponse;
        }

        int ans = this.updateById(user);
        if(ans == 0)
        {
            myResponse.setMsg("The account doesn't exist");
            return myResponse;
        }
        myResponse.setState(true);
        myResponse.setMsg("Update successfully");
        myResponse.setData(user);
        return myResponse;
    }

    default MyResponse<User> loginUser(User user)
    {
        MyResponse<User> myResponse = new MyResponse<>();
        myResponse.setState(false);
        try {
            User tmp = getByMail(user.getMailAddr());
            myResponse.setData(tmp);
            if(!tmp.getPwd().equals(user.getPwd()))
            {
                myResponse.setMsg("The password is not correct");
                return myResponse;
            }
        }catch (NullPointerException e)
        {
            myResponse.setMsg("The account doesn't exist");
            return myResponse;
        }
        myResponse.setState(true);
        return myResponse;
    }


}
